package com.cbtx.tablet.adapter

import android.graphics.Bitmap
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.cbtx.tablet.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/6 10:43 PM
 * desc   :
 */
class ImageDetailAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_goods_detail) {
    override fun convert(holder: BaseViewHolder, item: String) {

        val mIvDetail: ImageView = holder.getView(R.id.mIvDetail)

//        Glide.with(context)
//            .load(item)
//            .placeholder(R.mipmap.ic_default_big)
//            .listener(object : RequestListener<String, GlideDrawable> {
//                override fun onException(
//                    e: Exception?,
//                    model: String?,
//                    target: Target<GlideDrawable>?,
//                    isFirstResource: Boolean
//                ): Boolean {
//                    return false;
//                }
//
//                override fun onResourceReady(
//                    resource: GlideDrawable?,
//                    model: String?,
//                    target: Target<GlideDrawable>?,
//                    isFromMemoryCache: Boolean,
//                    isFirstResource: Boolean
//                ): Boolean {
//
//                    var w = 0  //图片原始宽度
//                    var h = 0 //图片原始高度
//                    resource?.apply {
//                        w = this.intrinsicWidth
//                        h = this.intrinsicHeight
//                    }
//
//                    val layoutParams = mIvDetail.layoutParams
//                    if (w <= 1050) {
//                        layoutParams.height = h
//                        layoutParams.width = w
//                    } else {
//                        layoutParams.width = 1050
//                        layoutParams.height = 1050 * h / w
//                    }
//                    return false
//                }
//            })
//            .into(mIvDetail)


        Glide.with(context)
            .asBitmap()
            .load(item)
            .placeholder(R.mipmap.ic_default_big)
            .into(object : SimpleTarget<Bitmap?>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
                    val w = resource.width //图片原始宽度
                    val h = resource.height //图片原始高度
                    val layoutParams = mIvDetail.layoutParams
                    if (w <= 1050) {
                        layoutParams.height = h
                        layoutParams.width = w
                    } else {
                        layoutParams.width = 1050
                        layoutParams.height = 1050 * h / w
                    }
                    mIvDetail.layoutParams = layoutParams
                    mIvDetail.setImageBitmap(resource)
                }
            })
    }

}